/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.eam.gui;

import edu.eam.dao.DAOAdmon;
import edu.eam.dao.DAOTecnologia;
import edu.eam.modelo.Tecnologia;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author TecnoMakro
 */
public class AdministrarTecnologias extends javax.swing.JFrame {

    /**
     * Creates new form AdministrarTecnologias2
     */
    public AdministrarTecnologias() {
        initComponents();
        ponerTecnologias();
        this.setTitle("Adminsitrar Tecnologias");
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        listTecnologias = new java.awt.List();
        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        TFNombreTecnologia = new java.awt.TextField();
        label3 = new java.awt.Label();
        TADescripcion = new java.awt.TextArea();
        BCrear = new java.awt.Button();
        BEditar = new java.awt.Button();
        BCancelar = new java.awt.Button();
        lblId = new java.awt.Label();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        listTecnologias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listTecnologiasActionPerformed(evt);
            }
        });

        label1.setText("Lista de tecnologias");

        label2.setText("Nombre");

        TFNombreTecnologia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TFNombreTecnologiaActionPerformed(evt);
            }
        });

        label3.setText("Descripcion");

        BCrear.setLabel("Crear");
        BCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCrearActionPerformed(evt);
            }
        });

        BEditar.setLabel("Editar");
        BEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BEditarActionPerformed(evt);
            }
        });

        BCancelar.setLabel("Cancelar");
        BCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelarActionPerformed(evt);
            }
        });

        lblId.setVisible(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(listTecnologias, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(TFNombreTecnologia, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(TADescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(53, 53, 53)
                                .addComponent(BEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(36, 36, 36)
                                .addComponent(BCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(110, 110, 110)
                        .addComponent(lblId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(listTecnologias, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TFNombreTecnologia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(TADescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(BCrear, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(BEditar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(BCancelar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(22, 22, 22))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void TFNombreTecnologiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TFNombreTecnologiaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TFNombreTecnologiaActionPerformed

    private void BEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BEditarActionPerformed
        
        Connection conex = DAOAdmon.crearConeccion();
        try {
            DAOTecnologia daoTec = new DAOTecnologia(conex);
            
            Tecnologia tec = new Tecnologia(TFNombreTecnologia.getText(), TADescripcion.getText(), 
                    Integer.parseInt(lblId.getText()));
        
        daoTec.modificarTecnologia(tec);
        JOptionPane.showMessageDialog(null, "Se ha modificado los datos de la tecnologia " + TFNombreTecnologia.getText());
        
        } catch(Exception e){
            Logger.getLogger(AdministrarTecnologias.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            try {
                conex.close();
            } catch (SQLException ex) {
                Logger.getLogger(AdministrarTecnologias.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_BEditarActionPerformed

    private void listTecnologiasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listTecnologiasActionPerformed
        // TODO add your handling code here:
        Connection conex = DAOAdmon.crearConeccion();
        try {
            Tecnologia tec =  new DAOTecnologia(conex).buscarTecnologia(listTecnologias.getSelectedItem());
            
            TFNombreTecnologia.setText(tec.getNombre());
            TADescripcion.setText(tec.getDescripcion());
            lblId.setText(tec.getId() + "");
        } catch (SQLException ex) {
            Logger.getLogger(AdministrarTecnologias.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                conex.close();
            } catch (SQLException ex) {
                Logger.getLogger(AdministrarTecnologias.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }//GEN-LAST:event_listTecnologiasActionPerformed

    private void BCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCrearActionPerformed
       
        Connection conex =  DAOAdmon.crearConeccion();
        try {
            // TODO add your handling code here:
            Tecnologia tec = new Tecnologia(TFNombreTecnologia.getText(), TADescripcion.getText());
            
            DAOTecnologia daoTec = new DAOTecnologia(conex);
            
            if (daoTec.buscarTecnologia(TFNombreTecnologia.getText()) != null) {
                
                JOptionPane.showMessageDialog(null, "La tecnologia con nombre " + TFNombreTecnologia.getText() +
                         " ya existe en la base de datos");
            } else {
                
                daoTec.crearTecnologia(tec);
                JOptionPane.showMessageDialog(null, "Se ha creado la tecnologia " + TFNombreTecnologia.getText());
                listTecnologias.clear();
                this.ponerTecnologias();
            }
        } catch (SQLException ex) {
            Logger.getLogger(AdministrarTecnologias.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                conex.close();
            } catch (SQLException ex) {
                Logger.getLogger(AdministrarTecnologias.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }//GEN-LAST:event_BCrearActionPerformed

    private void BCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_BCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */

        
        
        
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AdministrarTecnologias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AdministrarTecnologias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AdministrarTecnologias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AdministrarTecnologias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AdministrarTecnologias().setVisible(true);
            }
        });
    }
    
    
      /**
     * Metodoq ue realiza las instancias 
     * necesarias para mostrar las tecnolgoias
     * @return Lista de tecnologias
     */
    public void ponerTecnologias(){
       
        try {
            Connection conex = DAOAdmon.crearConeccion();
            
            List<Tecnologia> tecnologias = new DAOTecnologia(conex).darTecnologias();
            
            for (int i = 0; i < tecnologias.size(); i++) {
                
                
                listTecnologias.add(tecnologias.get(i).toString());
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(AdministrarTecnologias.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button BCancelar;
    private java.awt.Button BCrear;
    private java.awt.Button BEditar;
    private java.awt.TextArea TADescripcion;
    private java.awt.TextField TFNombreTecnologia;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.Label lblId;
    private java.awt.List listTecnologias;
    // End of variables declaration//GEN-END:variables
}
